package business_opportunity

import (
	"context"
	"git.mycaigou.com/gfyx/common/authorize"
	"git.mycaigou.com/gfyx/common/rpc_client"
	"git.mycaigou.com/gfyx/micro-gfyx-mp-api/infrastructure/common/constants"
	microGfyxOppotyService "git.mycaigou.com/gfyx/micro-gfyx-mp-api/proto/micro_gfyx_oppty_service_proto/micro-gfyx-oppty-service/business_opportunity"
	"git.mycaigou.com/gfyx/micro-gfyx-mp-api/service/business_opportunity/common"
)

type opptyStatusRepository struct {
}

// NewOpptyStatusRepository 报备
func NewOpptyStatusRepository() common.CommonRepositoryIface {
	return &opptyStatusRepository{}
}

func (r *opptyStatusRepository) GetOpptyStatus(ctx context.Context, request *microGfyxOppotyService.GetOpptyStatusRequest) (*microGfyxOppotyService.GetOpptyStatusResponse, error) {
	// 如果是未付费用户
	tenantCode := authorize.GetTenantCode(ctx)
	if tenantCode == "" {
		return &microGfyxOppotyService.GetOpptyStatusResponse{}, nil
	}

	conn, ctx, err := rpc_client.GetClientConn(ctx, constants.MICRO_GFYX_OPPTY_SERVICE)
	if err != nil {
		return nil, err
	}
	defer func() {
		_ = conn.Close()
	}()
	client := microGfyxOppotyService.NewOpptyStatusServiceClient(conn)
	return client.GetOpptyStatus(ctx, request)
}

func (r *opptyStatusRepository) GetOpptyStatusBatch(ctx context.Context, request *microGfyxOppotyService.GetOpptyStatusBatchRequest) (*microGfyxOppotyService.GetOpptyStatusBatchResponse, error) {
	// 如果是未付费用户
	tenantCode := authorize.GetTenantCode(ctx)
	if tenantCode == "" {
		return &microGfyxOppotyService.GetOpptyStatusBatchResponse{}, nil
	}

	conn, ctx, err := rpc_client.GetClientConn(ctx, constants.MICRO_GFYX_OPPTY_SERVICE)
	if err != nil {
		return nil, err
	}
	defer func() {
		_ = conn.Close()
	}()
	client := microGfyxOppotyService.NewOpptyStatusServiceClient(conn)
	return client.GetOpptyStatusBatch(ctx, request)
}

func (r *opptyStatusRepository) UpdateOpptyStatus(ctx context.Context, request *microGfyxOppotyService.UpdateOpptyStatusRequest) (*microGfyxOppotyService.UpdateOpptyStatusResponse, error) {
	// 如果是未付费用户
	tenantCode := authorize.GetTenantCode(ctx)
	if tenantCode == "" {
		return &microGfyxOppotyService.UpdateOpptyStatusResponse{}, nil
	}

	conn, ctx, err := rpc_client.GetClientConn(ctx, constants.MICRO_GFYX_OPPTY_SERVICE)
	if err != nil {
		return nil, err
	}
	defer func() {
		_ = conn.Close()
	}()
	client := microGfyxOppotyService.NewOpptyStatusServiceClient(conn)
	return client.UpdateOpptyStatus(ctx, request)
}
